package com.zzh.lib.views.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zzh.lib.views.HBannerViewPager;
import com.zzh.lib.views.adapter.holder.HSuperViewHolder;
import com.zzh.lib.xpopup.util.XPopupUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseBannerAdapter<T> extends RecyclerView.Adapter<HSuperViewHolder> {
    protected List<T> mList = new ArrayList<>();
    private boolean isCanLoop;
    public static final int MAX_VALUE = 1000;
    private HBannerViewPager.OnPageClickListener mPageClickListener;

    @NonNull
    @Override
    public final HSuperViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView =
                LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
        HSuperViewHolder viewHolder = createViewHolder(parent, itemView, viewType);
        itemView.setOnClickListener(clickedView -> {
            int adapterPosition = viewHolder.getAdapterPosition();
            if (mPageClickListener != null && adapterPosition != RecyclerView.NO_POSITION) {
                int realPosition =
                        XPopupUtils.getRealPosition(viewHolder.getAdapterPosition(), getListSize());
                mPageClickListener.onPageClick(clickedView, realPosition);
            }
        });
        return viewHolder;
    }

    @Override
    public final void onBindViewHolder(@NonNull HSuperViewHolder holder, final int position) {
        int realPosition = XPopupUtils.getRealPosition(position, getListSize());
        bindData(holder, mList.get(realPosition), realPosition, getListSize());
    }

    @Override
    public final int getItemViewType(int position) {
        int realPosition = XPopupUtils.getRealPosition(position, getListSize());
        return getViewType(realPosition);
    }

    @Override
    public final int getItemCount() {
        if (isCanLoop && getListSize() > 1) {
            return MAX_VALUE;
        } else {
            return getListSize();
        }
    }

    public List<T> getData() {
        return mList;
    }

    public void setData(List<? extends T> list) {
        if (null != list) {
            mList.clear();
            mList.addAll(list);
        }
    }

    public void setCanLoop(boolean canLoop) {
        isCanLoop = canLoop;
    }

    public void setPageClickListener(HBannerViewPager.OnPageClickListener pageClickListener) {
        mPageClickListener = pageClickListener;
    }

    public int getListSize() {
        return mList.size();
    }

    public  int getViewType(int position) {
        return 0;
    }

    public boolean isCanLoop() {
        return isCanLoop;
    }

    /**
     * Generally,there is no need to override this method in subclasses.
     * <p>
     * This method called by {@link #onCreateViewHolder(ViewGroup, int)} to create a default {@link
     * HSuperViewHolder}
     *
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param itemView Item View.
     * @param viewType The view type of the new View.
     * @return HSuperViewHolder extends {@link HSuperViewHolder}.
     */
    public HSuperViewHolder createViewHolder(@NonNull ViewGroup parent, View itemView,
                                             int viewType) {
        return new HSuperViewHolder(itemView);
    }

    /**
     * @param holder   The HSuperViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param data     Current item data.
     * @param position Current item position.
     * @param pageSize Page size of BVP,equals {@link BaseBannerAdapter#getListSize()}.
     */
    protected abstract void bindData(HSuperViewHolder holder, T data, int position, int pageSize);

    /**
     * @param viewType The view type of the new View.
     * @return The item view layout.
     */
    public abstract @LayoutRes int getLayoutId(int viewType);
}
